package com.cy.tempflinkcdc.util;

import lombok.extern.slf4j.Slf4j;
import net.lingala.zip4j.ZipFile;
import net.lingala.zip4j.exception.ZipException;
import net.lingala.zip4j.model.ZipParameters;
import net.lingala.zip4j.model.enums.CompressionMethod;
import net.lingala.zip4j.model.enums.EncryptionMethod;
import org.apache.flink.shaded.netty4.io.netty.util.internal.StringUtil;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.Struct;
import java.util.zip.Deflater;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

import static com.cy.tempflinkcdc.util.FileOperateUtil.deleteFolder;


@Slf4j
public class ZipUtil {

    /**
     * 遍历压缩文件夹
     *
     * @param folderPath
     * @param password
     * @throws IOException
     * @throws ZipException
     */
    public static void zipFolder(String folderPath, String password) throws IOException, ZipException {

        if (!StringUtil.isNullOrEmpty(folderPath)) {
            File pathFile = new File(folderPath);
            if (pathFile.exists()) {
                if (pathFile.isDirectory()) {
                    try {
                        recursiveDeflateZip(pathFile, password);
                    } catch (ZipException e) {
                        throw new ZipException("文件夹压缩失败");
                    }
                } else {
                    try {
                        singleDeflateZip(pathFile, password);

                    } catch (ZipException e) {
                        throw new ZipException("文件压缩失败");
                    }
                }
            }

        }

    }


    private static void zipFolder(String folderPath) throws ZipException, IOException {
        zipFolder(folderPath, ""); // 不设置密码的压缩文件夹
    }


    /**
     * 遍历压缩文件夹  并删除文件夹
     *
     * @param folderPath
     * @param deleteOriginFolder
     */
    public static void zipFolder(String folderPath, Boolean deleteOriginFolder) throws IOException, ZipException {
        zipFolder(folderPath);  //压缩
        // if (deleteOriginFolder) {
        //     deleteFolder(folderPath);  //删除文件夹
        // }
    }

    /**
     * 遍历压缩文件夹  并删除文件夹
     *
     * @param folderPath
     * @param deleteOriginFolder
     */
    public static void zipFolder(String folderPath, Boolean deleteOriginFolder, String password) throws IOException, ZipException {
        zipFolder(folderPath, password);  //压缩
        if (deleteOriginFolder) {
            deleteFolder(folderPath);  //删除文件夹
        }
    }





    public static String recursiveDeflateZip(File pathFile, String password) throws ZipException, net.lingala.zip4j.exception.ZipException {
        ZipParameters zipParameters = new ZipParameters();
        zipParameters.setEncryptFiles(true);
        zipParameters.setCompressionMethod(CompressionMethod.DEFLATE);//设置压缩方法是deflate
        zipParameters.setEncryptionMethod(EncryptionMethod.ZIP_STANDARD);
        new ZipFile(pathFile + ".zip", password.toCharArray()).addFolder(pathFile, zipParameters);
        return pathFile + ".zip";
    }

    public static void singleDeflateZip(File pathFile, String password) throws ZipException {
        ZipParameters zipParameters = new ZipParameters();
        zipParameters.setCompressionMethod(CompressionMethod.DEFLATE);//设置压缩方法是deflate
        zipParameters.setEncryptFiles(true);
        zipParameters.setEncryptionMethod(EncryptionMethod.ZIP_STANDARD);
        new ZipFile(pathFile + ".zip", password.toCharArray()).addFile(pathFile, zipParameters);
    }

}
